public class LeetCode141 {


    public static void main(String[] args) {

    }

    public static boolean hasCycle(ListNode head){

        //快慢指针
        ListNode fast = head;
        ListNode slow = head;
        boolean re  = false;
        while (true){

            //只对快指针进行判断为空则说明没有环
            if (fast == null || fast.next == null){
                break;
            }
            //前进
            fast = fast.next.next;
            slow = slow.next;
            //相等则说明存在环
            if (fast == slow){
                re = true;
                break;
            }

        }
        return re;
    }



      private static class ListNode {
      int val;
     ListNode next;
      ListNode(int x) {
     val = x;
         next = null;
      }
  }
}
